<?php
	/*
	Plugin Name: ToboAds plugin
	Description: Adds ToboAds before/after posts - ToboAds (www.toboads.com) by Tobonet (www.tobonet.com)
	Version: 1.2
	Author: Domen Kosir domen.kosir@tobonet.com
	Author URI: http://domn.sopca.com
	*/

	/**
	This file is part of ToboAds plugins.
	
	ToboAds plugins is free software; you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation; either version 2.1 of the License, or
	(at your option) any later version.
	
	ToboAds plugins is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with WideImage; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
	**/



// ----------------
// global variables
$toboads_domain = 'toboads';
if (!isset($toboads_counter))
	$toboads_counter = 0;
if (!isset($toboads_counter_max))
	$toboads_counter_max = 3;
if (!isset($toboads_allowed_ad_sizes))
	$toboads_allowed_ad_sizes = array('180x150','234x60','300x250','336x280','468x60','728x90','120x600','120x240','160x240','125x125','160x600','250x250');



// ----------------
// validates the toboads publisher id
if (!function_exists('toboads_is_valid_publisher_id'))
{
	function toboads_is_valid_publisher_id($publisher_id)
	{
		if (preg_match("/^[0-9a-f]{32}$/", $publisher_id) > 0)
			return true;
		return false;
	}
}



// ----------------
// validates colors
if (!function_exists('toboads_validate_color'))
{
	function toboads_validate_color($color)
	{
		if ((strlen($color) > 0) && ($color{0} != '#')) $color = '#' . $color;
		if (preg_match("/^#[0-9a-f]{6}$/i", $color) > 0) // RGB code
			return $color;
		return '';
	}
}



// ----------------
// validates channel
if (!function_exists('toboads_validate_channel'))
{
	function toboads_validate_channel($channel)
	{
		if (preg_match("/^[0-9a-f]{7}$/", $channel) > 0)
			return $channel;
		return '';
	}
}



// ----------------
// adds toboads to excerpts
function toboads_filter_excerpt($content)
{
	global $wpdb;
	global $toboads_counter, $toboads_counter_max;
	
	$options = get_option('toboads_plugin');
	
	// publisher_id
	$publisher_id = $options['publisher_id'];
	// places
	$place_before = $options['place_before_excerpt'];
	$place_after = $options['place_after_excerpt'];
	
	if ($toboads_counter < $toboads_counter_max && toboads_is_valid_publisher_id($publisher_id))
	{
		if ($place_before == 1)
		{
			$content = toboads_generate_javascript($options) . $content;
			$toboads_counter++;
		}
		
		if ($place_after)
		{
			$content = $content . toboads_generate_javascript($options);
			$toboads_counter++;
		}
	}
	
	return $content;
}



// ----------------
// adds toboads to contents
function toboads_filter_content($content)
{
	global $wpdb;
	global $toboads_counter, $toboads_counter_max;
	
	$options = get_option('toboads_plugin');
	
	// publisher_id
	$publisher_id = $options['publisher_id'];
	// places
	$place_before = $options['place_before_content'];
	$place_after = $options['place_after_content'];
	
	if ($toboads_counter < $toboads_counter_max && toboads_is_valid_publisher_id($publisher_id))
	{
		if ($place_before == 1)
		{
			$content = toboads_generate_javascript($options) . $content;
			$toboads_counter++;
		}
		
		if ($place_after)
		{
			$content = $content . toboads_generate_javascript($options);
			$toboads_counter++;
		}
	}
	
	return $content;
}



// ----------------
// generates toboads javascript code
function toboads_generate_javascript($options)
{
	global $toboads_allowed_ad_sizes;
	
	// publisher_id
	$publisher_id = $options['publisher_id'];
	// size
	$ad_size = $options['ad_size'];
	if (!in_array($ad_size, $toboads_allowed_ad_sizes)) $ad_size = '120x240';
	// open_new_window
	$options['open_new_window'] = ($options['open_new_window'] === '1' ? '1' : '0');
	$open_new_window = $options['open_new_window'];
	// type
	$options['type_text'] = ($options['type_text'] !== '0' ? '1' : '0');
	$type_text = $options['type_text'];
	$options['type_shop'] = ($options['type_shop'] !== '0' ? '1' : '0');
	$type_shop = $options['type_shop'];
	$options['type_rich'] = ($options['type_rich'] !== '0' ? '1' : '0');
	$type_rich = $options['type_rich'];
	// colors
	$color_border = toboads_validate_color($options['color_border']);
	$color_bg = toboads_validate_color($options['color_bg']);
	$color_title = toboads_validate_color($options['color_title']);
	$color_text = toboads_validate_color($options['color_text']);
	$color_link = toboads_validate_color($options['color_link']);
	// channel
	$channel = $options['channel'];
	
	// generate javascript string
	$string = "<div align=\"center\">\n";
	$string .= "<!-- ToboAds -->\n";
	$string .= "<script language=\"javascript\" type=\"text/javascript\">\n";
	$string .= "// <![CDATA[\n";
	// publisher_id
	$string .= "	toboads_publisher_id = '$publisher_id';\n";
	$string .= "	toboads_ad_format = '$ad_size';\n";
	// open_new_window
	$string .= "	toboads_open_new_window = '".($open_new_window == "1" ? "true" : "false")."';\n";
	// type
	$string .= "	toboads_ad_type_text = '".($type_text == "1" ? "true" : "false")."';\n";
	$string .= "	toboads_ad_type_shop = '".($type_shop == "1" ? "true" : "false")."';\n";
	$string .= "	toboads_ad_type_rich = '".($type_rich == "1" ? "true" : "false")."';\n";
	// colors
	if (strlen($color_border) > 0)
		$string .= "	toboads_color_border = '$color_border';\n";
	if (strlen($color_bg) > 0)
		$string .= "	toboads_color_bg = '$color_bg';\n";
	if (strlen($color_title) > 0)
		$string .= "	toboads_color_title = '$color_title';\n";
	if (strlen($color_link) > 0)
		$string .= "	toboads_color_link = '$color_link';\n";
	if (strlen($color_text) > 0)
		$string .= "	toboads_color_text = '$color_text';\n";
	//channel
	$string .= "	toboads_channel = '$channel';\n";
	
	$string .= "// ]]>\n";
	$string .= "</script>\n";
	$string .= "<script language=\"javascript\" type=\"text/javascript\" src=\"http://relay-si.toboads.com/\"></script>\n";
	$string .= "<!-- /ToboAds -->\n";
	$string .= "</div>\n";
	
	return $string;
}




// ----------------
// ads ToboAds to menu
function toboads_menu()
{
	if (function_exists('add_options_page'))
	{
		add_options_page('subpaneltitle', 'ToboAds', 1, basename(__FILE__), 'toboads_subpanel');
	}
}



// ----------------
// toboads options
function toboads_subpanel()
{
	global $toboads_allowed_ad_sizes;
	
	$options = get_option('toboads_plugin');
	
	echo '<div class="wrap">';
	echo '<h2>ToboAds Options</h2>';
	
	if ($_POST['action'] == 'update_toboads_options')
	{
		// publisher_ id
		if (toboads_is_valid_publisher_id($_POST['toboads-publisher_id'])) $options['publisher_id'] = $_POST['toboads-publisher_id'];
		// size
		$options['ad_size'] = $_POST['toboads-ad_size'];
		if (!in_array($options['ad_size'], $toboads_allowed_ad_sizes)) $options['ad_size'] = '120x240';
		// open_new_window
		$options['open_new_window'] = $_POST['toboads-open_new_window'] == '1' ? '1' : '0';
		// type
		$options['type_text'] = ($_POST['toboads-type_text'] == 'on' ? '1' : '0');
		$options['type_shop'] = ($_POST['toboads-type_shop'] == 'on' ? '1' : '0');
		$options['type_rich'] = ($_POST['toboads-type_rich'] == 'on' ? '1' : '0');
		// colors
		$options['color_border'] = toboads_validate_color($_POST['toboads-color_border']);
		$options['color_bg'] = toboads_validate_color($_POST['toboads-color_bg']);
		$options['color_title'] = toboads_validate_color($_POST['toboads-color_title']);
		$options['color_text'] = toboads_validate_color($_POST['toboads-color_text']);
		$options['color_link'] = toboads_validate_color($_POST['toboads-color_link']);
		// channel
		$options['channel'] = toboads_validate_channel($_POST['toboads-channel']);
		
		// place
		if ($_POST['toboads-place_before_excerpt'] == 'on') $options['place_before_excerpt'] = '1';
		else $options['place_before_excerpt'] = '0';
		if ($_POST['toboads-place_after_excerpt'] == 'on') $options['place_after_excerpt'] = '1';
		else $options['place_after_excerpt'] = '0';
		if ($_POST['toboads-place_before_content'] == 'on') $options['place_before_content'] = '1';
		else $options['place_before_content'] = '0';
		if ($_POST['toboads-place_after_content'] == 'on') $options['place_after_content'] = '1';
		else $options['place_after_content'] = '0';
		
		update_option('toboads_plugin', $options);
		echo "<div class='updated'><p>Options updated.</p></div>";
	}
	
	// publisher_id
	$publisher_id = $options['publisher_id'];
	// size
	$ad_size = $options['ad_size'];
	if (!in_array($ad_size, $toboads_allowed_ad_sizes)) $ad_size = '120x240';
	// open_new_window
	$options['open_new_window'] = ($options['open_new_window'] === '1' ? '1' : '0');
	$open_new_window = $options['open_new_window'];
	// type
	$options['type_text'] = ($options['type_text'] !== '0' ? '1' : '0');
	$type_text = $options['type_text'];
	$options['type_shop'] = ($options['type_shop'] !== '0' ? '1' : '0');
	$type_shop = $options['type_shop'];
	$options['type_rich'] = ($options['type_rich'] !== '0' ? '1' : '0');
	$type_rich = $options['type_rich'];
	// colors
	$color_border = $options['color_border'];
	$color_bg = $options['color_bg'];
	$color_title = $options['color_title'];
	$color_text = $options['color_text'];
	$color_link = $options['color_link'];
	// channel
	$channel = $options['channel'];
	// place
	$place_before_excerpt = $options['place_before_excerpt'];
	$place_after_excerpt = $options['place_after_excerpt'];
	$place_before_content = $options['place_before_content'];
	$place_after_content = $options['place_after_content'];
	
	echo '<h3>ToboAds Options</h3>';
	
	echo '<form method="post" action="options-general.php?page=toboads_plugin.php">';
	echo '<input type="hidden" name="action" value="update_toboads_options" />';
	echo '<table border="0" cellspacing="4" cellpadding="1">';
	
	// publisher_id
	echo '<tr>';
	echo '<td align="right"><label for="toboads-publisher_id">Publisher ID: </label></td>';
	echo '<td align="left"><input style="width: 270px;" maxlength="32" id="toboads-publisher_id" name="toboads-publisher_id" type="text" value="'.$publisher_id.'" /></td>';
	echo '</tr>';
	// size
	echo '<tr>';
	echo '<td align="right"><label for="toboads-ad_size">Ad size: </label></td>';
	echo '<td align="left"><select id="toboads-ad_size" name="toboads-ad_size">'; foreach($toboads_allowed_ad_sizes as $tmp_ad_size) { if ($ad_size == $tmp_ad_size) $sel = 'selected="selected"'; else $sel = ''; echo '<option value="'.$tmp_ad_size.'" '.$sel.'>'.$tmp_ad_size.'</option>'; } echo '</select></td>';
	echo '</tr>';
	// open_new_window
	echo '<tr>';
	echo '<td align="right">Open in new window: </td>';
	echo '<td align="left">';
	echo '<input type="radio" id="toboads-open_new_window_1" name="toboads-open_new_window" value="1" '.($open_new_window == 1 ? 'checked="checked"' : '').' /> <label for="toboads-open_new_window_1">new window</label>';
	echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	echo '<input type="radio" id="toboads-open_new_window_0" name="toboads-open_new_window" value="0" '.($open_new_window == 0 ? 'checked="checked"' : '').' /> <label for="toboads-open_new_window_0">same window</label>';
	echo '</td>';
	echo '</tr>';
	// type
	echo '<tr>';
	echo '<td align="right">Ad type: </td>';
	echo '<td align="left">';
	if ($type_text == 1) $type_text_chk = 'checked="checked"';
	else $type_text_chk = '';
	echo '<input type="checkbox" id="toboads-type_text" name="toboads-type_text" value="on" '.$type_text_chk.' /> <label for="toboads-type_text">text</label>';
	echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	if ($type_shop == 1) $type_shop_chk = 'checked="checked"';
	else $type_shop_chk = '';
	echo '<input type="checkbox" id="toboads-type_shop" name="toboads-type_shop" value="on" '.$type_shop_chk.' /> <label for="toboads-type_shop">shop</label>';
	echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	if ($type_rich == 1) $type_rich_chk = 'checked="checked"';
	else $type_rich_chk = '';
	echo '<input type="checkbox" id="toboads-type_rich" name="toboads-type_rich" value="on" '.$type_rich_chk.' /> <label for="toboads-type_rich">rich</label>';
	echo '</td>';
	echo '</tr>';
	// colors
	echo '<tr>';
	echo '<td align="right">Border color: </td>';
	echo '<td align="left"><input style="width: 70px;" id="toboads-color_border" name="toboads-color_border" type="text" value="'.$color_border.'" /></td>';
	echo '</tr>';
	echo '<tr>';
	echo '<td align="right">Background color: </td>';
	echo '<td align="left"><input style="width: 70px;" id="toboads-color_bg" name="toboads-color_bg" type="text" value="'.$color_bg.'" /></td>';
	echo '</tr>';
	echo '<tr>';
	echo '<td align="right">Title color: </td>';
	echo '<td align="left"><input style="width: 70px;" id="toboads-color_title" name="toboads-color_title" type="text" value="'.$color_title.'" /></td>';
	echo '</tr>';
	echo '<tr>';
	echo '<td align="right">Text color: </td>';
	echo '<td align="left"><input style="width: 70px;" id="toboads-color_text" name="toboads-color_text" type="text" value="'.$color_text.'" /></td>';
	echo '</tr>';
	echo '<tr>';
	echo '<td align="right">Link color: </td>';
	echo '<td align="left"><input style="width: 70px;" id="toboads-color_link" name="toboads-color_link" type="text" value="'.$color_link.'" /></td>';
	echo '</tr>';
	// channel
	echo '<tr>';
	echo '<td align="right">Channel: </td>';
	echo '<td align="left"><input style="width: 70px;" id="toboads-channel" name="toboads-channel" type="text" value="'.$channel.'" /></td>';
	echo '</tr>';
	// place
	echo '<tr>';
	echo '<td align="right">Place ads: </label></td>';
	echo '<td align="left">';
	if ($place_before_excerpt == 1) $place_before_excerpt_chk = 'checked="checked"';
	else $place_before_excerpt_chk = '';
	echo '<input type="checkbox" id="toboads-place_before_excerpt" name="toboads-place_before_excerpt" value="on" '.$place_before_excerpt_chk.' /> <label for="toboads-place_before_excerpt">before excerpts</label>';
	echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	if ($place_after_excerpt == 1) $place_after_excerpt_chk = 'checked="checked"';
	else $place_after_excerpt_chk = '';
	echo '<input type="checkbox" id="toboads-place_after_excerpt" name="toboads-place_after_excerpt" value="on" '.$place_after_excerpt_chk.' /> <label for="toboads-place_after_excerpt">after excerpts</label>';
	echo '<br />';
	if ($place_before_content == 1) $place_before_content_chk = 'checked="checked"';
	else $place_before_content_chk = '';
	echo '<input type="checkbox" id="toboads-place_before_content" name="toboads-place_before_content" value="on" '.$place_before_content_chk.' /> <label for="toboads-place_before_content">before contents</label>';
	echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	if ($place_after_content == 1) $place_after_content_chk = 'checked="checked"';
	else $place_after_content_chk = '';
	echo '<input type="checkbox" id="toboads-place_after_content" name="toboads-place_after_content" value="on" '.$place_after_content_chk.' /> <label for="toboads-place_after_content">after contents</label>';
	echo '</td>';
	echo '</tr>';
	
	echo '</table>';
	echo '<div style="text-align: right;">';
	echo '<input type="submit" class="button" value="Update options">';
	echo '</div>';
	echo '</form>';
	
	echo '</div>';
	
	echo '<div class="wrap">';
	echo '<p>All colors must be in <a href="http://en.wikipedia.org/wiki/Web_colors" target="_blank">hexadecimal</a> format. The hash character (<code>#</code>) is optional.</p>';
	echo '<p>The number of ToboAds boxes per page is limited to 3.</p>';
	echo '</div>';
}


add_filter('the_content', 'toboads_filter_content');
add_filter('the_excerpt', 'toboads_filter_excerpt');

add_action('admin_menu', 'toboads_menu');

?>
